package com.warmlight.voicepacket.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.base.BaseFragment;
import com.warmlight.voicepacket.ui.imlm.adapter.MyIMMessageAdapter;
import com.warmlight.voicepacket.ui.user.usercenter.LookUserCenterActivity;
import com.warmlight.voicepacket.utils.ImConnect;

import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imkit.model.UIConversation;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;

/**
 * 聊天消息
 */
public class MyIMMessageFragment extends BaseFragment {

    private SmartRefreshLayout refreshLayout;
    private RecyclerView rvMymessagelist;
    //    private ListAdapter<AuctionInfoViewModel> mAdapter;//拍场适配器

    //下拉刷新组件
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private TextView mEmptyView;   // 空白视图
    private MyIMMessageAdapter adapter;

    public static MyIMMessageFragment newInstance(int tag) {
        MyIMMessageFragment f = new MyIMMessageFragment();

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_conversationlist, null);
        ConversationListFragment fragment = new ConversationListFragment();
        Uri uri = Uri.parse("rong://" + getContext().getApplicationInfo().packageName).buildUpon()
                .appendPath("conversationlist")
                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话，该会话聚合显示
                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")//设置群组会话，该会话非聚合显示
                .build();
        fragment.setUri(uri);  //设置 ConverssationListFragment 的显示属性

        FragmentTransaction transaction =getActivity().getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.rong_content, fragment);
        transaction.commit();
//        initview(v);
        RongIM.setOnReceiveMessageListener(new RongIMClient.OnReceiveMessageListener() {
            @Override
            public boolean onReceived(Message message, int i) {
                String id=message.getSenderUserId();
                ImConnect.getInstance().SetUserById(id);
                return false;
            }
        });


        RongIM.setConversationListBehaviorListener(new RongIM.ConversationListBehaviorListener() {
            @Override
            public boolean onConversationPortraitClick(Context context, Conversation.ConversationType conversationType, String s) {

                Intent intent = new Intent(context, LookUserCenterActivity.class);
                intent.putExtra("uid", s);
                context.startActivity(intent);

                return true;
            }

            @Override
            public boolean onConversationPortraitLongClick(Context context, Conversation.ConversationType conversationType, String s) {
                return false;
            }

            @Override
            public boolean onConversationLongClick(Context context, View view, UIConversation uiConversation) {
                return false;
            }

            @Override
            public boolean onConversationClick(Context context, View view, UIConversation uiConversation) {
                return false;
            }
        });


        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        RongIM.getInstance().getConversationList(new RongIMClient.ResultCallback<List<Conversation>>() {
            @Override
            public void onSuccess(List<Conversation> conversations) {
                if (conversations != null && conversations.size()>0) {
                    for (Conversation c : conversations) {
                        ImConnect.getInstance().SetUserById(c.getTargetId());
                    }
                }
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {

            }
        }, Conversation.ConversationType.PRIVATE);
    }

    //
//    @Override
//    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
//        super.onViewCreated(view, savedInstanceState);
//        rvMymessagelist.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
//        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                refreshLayout.finishRefresh();
//            }
//        });
//
//        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//
//                refreshLayout.finishLoadMore();
//            }
//        });
//
//        adapter=new MyIMMessageAdapter(getContext());
//        rvMymessagelist.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
//        rvMymessagelist.setAdapter(adapter);
//    }
//
//    private void initview(View v) {
//
//        refreshLayout = (SmartRefreshLayout) v.findViewById(R.id.refreshLayout);
//        rvMymessagelist = (RecyclerView) v.findViewById(R.id.rv_mymessagelist);
//
//    }


}
